﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Manager.ClothManager
{
    public partial class ClothAdd : System.Web.UI.Page
    {
        protected readonly BLL.ClothBLL clothbll = new BLL.ClothBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                ViewState["Back"] = Request.UrlReferrer.ToString();
                string cmdstr = "";
                //布料公司
                cmdstr = "select * from ClothCompany order by sequence asc";
                SqlHelper.BindDropDownList(ddlCompany, "shortName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                //幅宽
                cmdstr = "select * from WidthKind";
                SqlHelper.BindDropDownList(ddlWidth,"widthKind","ID",SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null);
                
                //价格单位 
                cmdstr = "select * from PriceUnit";
                SqlHelper.BindDropDownList(ddlUnit,"unitName","ID",SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null);
                ddlUnit.SelectedValue = "2";

                if (Request["clothid"] != null)
                {
                    initData(Request["clothid"].ToString());
                }
                
            }
        }
        //初始化要修改的数据
        private void initData(string clothid)
        {
            DataTable dt=clothbll.getClothViewByID(clothid);
            //布料公司和布料号码不可更改
            ddlCompany.SelectedValue = dt.Rows[0]["companyID"].ToString();
            ddlCompany.Enabled = false;
            clothNo.Text = dt.Rows[0]["clothNo"].ToString();
            clothNo.Enabled = false;
            ddlWidth.SelectedValue = dt.Rows[0]["widthID"].ToString();
            ddlUnit.SelectedValue = dt.Rows[0]["unitID"].ToString();
            clothPrice.Text = dt.Rows[0]["price"].ToString();
            clothColor.Text = dt.Rows[0]["clothColor"].ToString();
            quality.Text = dt.Rows[0]["clothQuality"].ToString();
            remark.InnerText = dt.Rows[0]["remark"].ToString();
        }

        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Albon.Model.Cloth cloth = new Albon.Model.Cloth();
            cloth.ID = 0;
            if (Request["clothid"] != null)
            {
                cloth.ID = int.Parse(Request["clothid"].ToString());
            }
            cloth.companyID =int.Parse(ddlCompany.SelectedValue);
            cloth.clothNo = clothNo.Text.Trim();
            cloth.widthID =int.Parse(ddlWidth.SelectedValue);
            cloth.clothPrice = decimal.Parse(clothPrice.Text);
            cloth.unitID = int.Parse(ddlUnit.SelectedValue);
            cloth.clothColor = clothColor.Text.Trim();
            cloth.clothQuality = quality.Text.Trim();
            cloth.remark = remark.InnerText.Trim();
            //添加或修改布料
            clothbll.addCloth(cloth);
            Response.Redirect(ViewState["Back"].ToString());
        }
    }
}